﻿$(function(){//延迟加载
	var dvbelong=$('#loaddvbelong').val();
	if(dvbelong==''){
		dvbelong='olt';
	}
	var jxId=$('#loadjxId').val();
	$('#curfailurelist').datagrid({
		url:'getfailurelist',
		toolbar:'#tb2',
		queryParams:{
			dvbelong:dvbelong,
			jfId:'',
			jxId:jxId
		},
		pagination:true,
		columns:[[{
			field:'dvname',
			title:'设备名称',
			width:'10%'
		},{
			field:'dvip',
			title:'设备IP',
			width:'10%'
		},{
			field:'dvbelong',
			title:'设备类型',
			width:'10%'
		},{
			field:'djxname',
			title:'汇接',
			width:'10%'
		},{
			field:'juxiang',
			title:'局向',
			width:'10%'
		},{
			field:'jfname',
			title:'机房',
			width:'10%'
		},{
			field:'status',
			title:'设备状态',
			width:'10%'
		},{
			field:'recordfrom',
			title:'故障信息来源',
			width:'10%'
		},{
			field:'begintime',
			title:'故障起始时间',
			width:'13%',
			formatter:function(value,row,index){
        		if(value!=null&&value!=''){
        			var date = new Date(value);
        			Y = date.getFullYear() + '-';
        			M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        			D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
        			h =(date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
        			m =(date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
        			s =(date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds()); 
        			return Y+M+D+h+m+s;
        		}else{
        			return '';
        		}
        	}
		}]]
	});
	
	var pager = $('#curfailurelist').datagrid('getPager');	// get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-search',
			text:'设备详细拓扑',
			width:'200px',
			handler:function(){
				var secrow=$('#curfailurelist').datagrid('getSelected');
//				alert(secrow);
				if(secrow!=null){				
					var dvip=secrow.dvip;
					var dvname=secrow.dvname;
					var dvId=secrow.dvId;
					var dvbelong=secrow.dvbelong;
					parent.addTab(dvname+'('+dvip+')','tojxtp?dvIds='+dvbelong+','+dvId);	
				}
			}
		},{
			iconCls:'icon-search',
			text:'设备详细信息',
			width:'200px',
			handler:function(){
				var secrow=$('#curfailurelist').datagrid('getSelected');
				var dvname=secrow.dvname;
				var dvbelong=secrow.dvbelong;
				var dvId=secrow.dvId;
				parent.addTab(dvname,'toeditdv?dvId='+dvId+'&dvbelong='+dvbelong);
			}
		},{
			iconCls:'icon-redo',
			text:'导出EXCEL',
			width:'200px',
			handler:function(){
				 var rows = $('#curfailurelist').datagrid('getRows');  
		            var columns = $("#curfailurelist").datagrid("options").columns[0];  
		            var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel   
		            var oWB = oXL.Workbooks.Add(); //获取workbook对象   
		            var oSheet = oWB.ActiveSheet; //激活当前sheet  
		            //设置工作薄名称  
		            oSheet.name = "导出Excel报表";  
		            //设置表头  
		            for (var i = 0; i < columns.length; i++) {  
		                oSheet.Cells(1, i+1).value = columns[i].title;  
		            }  
		            //设置内容部分  
		            for (var i = 0; i < rows.length; i++) {  
		                //动态获取每一行每一列的数据值  
		                for (var j = 0; j < columns.length; j++) {                 
		                    oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];  
		                }     
		            }                
		            oXL.Visible = true; //设置excel可见属性  
			}
		}]
	});
	
	
	$('#dvbelong').combobox({
		onChange:function(newValue,oldValue){
			$('#curfailurelist').datagrid('reload',{
				dvbelong:newValue,
				jfId:'',
				jxId:''
			});
		}
	})
	
	$('#dajuxiang').combobox({
		onChange:function(newValue,oldValue){
			if(newValue==''){				
				$('#juxiang').combobox({					
					disabled:true
				});
				$('#juxiang').combobox('clear');
				$('#jifang').combobox({
					disabled:true
				});
				$('#jifang').combobox('clear');
			}else{
				$('#jifang').combobox({
					disabled:true
				});
				$('#juxiang').combobox({
					disabled:false,
					valueField: 'jxId',    
			        textField: 'jxname',
					url:"showjx?djxId="+newValue,
				});
			}
		}
	});
	$('#juxiang').combobox({
		onChange:function(newValue,oldValue){
			if(newValue==''){				
				$('#jifang').combobox({
					disabled:true
				});
				$('#jifang').combobox('clear');
			}else{
				$('#jifang').combobox({
					disabled:false,
					valueField:'jfId',
					textField:'jfname',
					url:'showjf?jxId='+newValue,
					onLoadSuccess:function(data){
						$('#curfailurelist').datagrid('reload',{
							dvbelong:$('#dvbelong').combobox('getValue'),
							jfId:'',
							jxId:newValue
						});
					}
				})
			}
			//$('#jxmap').src='creatjxmap?jxId='+newValue;
//			if(newValue!='')
//			 document.getElementById('jxmap').src="creatjxmap?jxId="+newValue;
		}
	});
	$('#jifang').combobox({
		width:200,
		onChange:function(newValue,oldValue){
			$('#curfailurelist').datagrid('reload',{
				dvbelong:$('#dvbelong').combobox('getValue'),
				jfId:newValue,
				jxId:''
			});
		}
	});
});